{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7afa37fe-be22-408f-8fe8-6e8609cf93bd",
   "metadata": {},
   "source": [
    "# pandas2 - DataFrame\n",
    "\n",
    "DataFrame是二维数据结构，类似于excel表格。DataFrame每一列都是一个Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "92c577f6-7ea4-4406-b6df-58d17f3ce017",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47067cd3-32d0-4395-b94e-9537016aeffc",
   "metadata": {},
   "source": [
    "## 直接创建DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8f1bd365-d16b-401b-9931-731186dac826",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1001</th>\n",
       "      <td>62</td>\n",
       "      <td>88</td>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1002</th>\n",
       "      <td>97</td>\n",
       "      <td>84</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1003</th>\n",
       "      <td>60</td>\n",
       "      <td>83</td>\n",
       "      <td>88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1004</th>\n",
       "      <td>80</td>\n",
       "      <td>91</td>\n",
       "      <td>71</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1005</th>\n",
       "      <td>85</td>\n",
       "      <td>61</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      语文  数学  英语\n",
       "1001  62  88  75\n",
       "1002  97  84  87\n",
       "1003  60  83  88\n",
       "1004  80  91  71\n",
       "1005  85  61  94"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过二维数组创建\n",
    "scores = np.random.randint(60, 101, (5, 3)) # 数据\n",
    "courses = ['语文', '数学', '英语'] # 列标题\n",
    "stu_ids = np.arange(1001, 1006) # 行索引\n",
    "df1 = pd.DataFrame(data=scores, columns=courses, index=stu_ids)\n",
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d312ef94-481b-4181-84c6-c3f1916ae5b2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1001</th>\n",
       "      <td>62</td>\n",
       "      <td>95</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1002</th>\n",
       "      <td>72</td>\n",
       "      <td>65</td>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1003</th>\n",
       "      <td>93</td>\n",
       "      <td>86</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1004</th>\n",
       "      <td>88</td>\n",
       "      <td>66</td>\n",
       "      <td>69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1005</th>\n",
       "      <td>93</td>\n",
       "      <td>87</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      语文  数学  英语\n",
       "1001  62  95  66\n",
       "1002  72  65  75\n",
       "1003  93  86  82\n",
       "1004  88  66  69\n",
       "1005  93  87  82"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过字典创建\n",
    "scores = {\n",
    "    '语文': [62, 72, 93, 88, 93],\n",
    "    '数学': [95, 65, 86, 66, 87],\n",
    "    '英语': [66, 75, 82, 69, 82],\n",
    "} # 键作为列标签\n",
    "stu_ids = np.arange(1001, 1006) # 行索引\n",
    "df2 = pd.DataFrame(data=scores, index=stu_ids)\n",
    "df2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c66cdaf-d987-4f6b-a607-9b2ffd9f1441",
   "metadata": {},
   "source": [
    "## 读取文件创建DataFrame"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3169a79-28dc-4e7c-bf29-36bbfd0ba666",
   "metadata": {},
   "source": [
    "### 读取CSV文件创建DataFrame对象\n",
    "\n",
    "可以通过pandas 模块的read_csv函数来读取 CSV 文件，read_csv函数的参数非常多，下面介绍几个比较重要的参数。\n",
    "\n",
    "- sep / delimiter：分隔符，默认是`,`\n",
    "- header：表头（列索引）的位置，默认值是infer，用第一行的内容作为表头（列索引）。\n",
    "- index_col：用作行索引（标签）的列。\n",
    "- usecols：需要加载的列，可以使用序号或者列名。\n",
    "- true_values / false_values：哪些值被视为布尔值True / False。\n",
    "- skiprows：通过行号、索引或函数指定需要跳过的行。\n",
    "- skipfooter：要跳过的末尾行数。\n",
    "- nrows：需要读取的行数。\n",
    "- na_values：哪些值被视为空值。\n",
    "- iterator：设置为True，函数返回迭代器对象。\n",
    "- chunksize：配合上面的参数，设置每次迭代获取的数据体量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "724c08a1-b175-4a87-8377-cf97493d0fde",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>birthday</th>\n",
       "      <th>company</th>\n",
       "      <th>score</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>杨效丰</td>\n",
       "      <td>1972-12</td>\n",
       "      <td>北京利德华福电气技术有限公司</td>\n",
       "      <td>122.59</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>纪丰伟</td>\n",
       "      <td>1974-12</td>\n",
       "      <td>北京航天数据股份有限公司</td>\n",
       "      <td>121.25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>王永</td>\n",
       "      <td>1974-05</td>\n",
       "      <td>品牌联盟(北京)咨询股份公司</td>\n",
       "      <td>118.96</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>杨静</td>\n",
       "      <td>1975-07</td>\n",
       "      <td>中科专利商标代理有限责任公司</td>\n",
       "      <td>118.21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>张凯江</td>\n",
       "      <td>1974-11</td>\n",
       "      <td>北京阿里巴巴云计算技术有限公司</td>\n",
       "      <td>117.79</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6015</th>\n",
       "      <td>孙宏波</td>\n",
       "      <td>1978-08</td>\n",
       "      <td>华为海洋网络有限公司北京科技分公司</td>\n",
       "      <td>90.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6016</th>\n",
       "      <td>刘丽香</td>\n",
       "      <td>1976-11</td>\n",
       "      <td>福斯（上海）流体设备有限公司北京分公司</td>\n",
       "      <td>90.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6017</th>\n",
       "      <td>周崧</td>\n",
       "      <td>1977-10</td>\n",
       "      <td>赢创德固赛（中国）投资有限公司</td>\n",
       "      <td>90.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6018</th>\n",
       "      <td>赵妍</td>\n",
       "      <td>1979-07</td>\n",
       "      <td>澳科利耳医疗器械（北京）有限公司</td>\n",
       "      <td>90.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6019</th>\n",
       "      <td>贺锐</td>\n",
       "      <td>1981-06</td>\n",
       "      <td>北京宝洁技术有限公司</td>\n",
       "      <td>90.75</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>6019 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     name birthday              company   score\n",
       "id                                             \n",
       "1     杨效丰  1972-12       北京利德华福电气技术有限公司  122.59\n",
       "2     纪丰伟  1974-12         北京航天数据股份有限公司  121.25\n",
       "3      王永  1974-05       品牌联盟(北京)咨询股份公司  118.96\n",
       "4      杨静  1975-07       中科专利商标代理有限责任公司  118.21\n",
       "5     张凯江  1974-11      北京阿里巴巴云计算技术有限公司  117.79\n",
       "...   ...      ...                  ...     ...\n",
       "6015  孙宏波  1978-08    华为海洋网络有限公司北京科技分公司   90.75\n",
       "6016  刘丽香  1976-11  福斯（上海）流体设备有限公司北京分公司   90.75\n",
       "6017   周崧  1977-10      赢创德固赛（中国）投资有限公司   90.75\n",
       "6018   赵妍  1979-07     澳科利耳医疗器械（北京）有限公司   90.75\n",
       "6019   贺锐  1981-06           北京宝洁技术有限公司   90.75\n",
       "\n",
       "[6019 rows x 4 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将id列作为行索引\n",
    "df3 = pd.read_csv('data/2018年北京积分落户数据.csv', index_col='id')\n",
    "df3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d525f2b-a0b0-48bb-986f-e6a9e2d8a752",
   "metadata": {},
   "source": [
    "### 读取Excel工作表创建DataFrame对象\n",
    "可以通过pandas 模块的read_excel函数来读取 Excel 文件，该函数与上面的read_csv非常类似，多了一个sheet_name参数来指定数据表的名称，但是不同于 CSV 文件，没有sep或delimiter这样的参数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c89760be-5ab1-4c4f-a1e6-48ed7ed6c277",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Open</th>\n",
       "      <th>High</th>\n",
       "      <th>Low</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2022-12-30</th>\n",
       "      <td>83.120</td>\n",
       "      <td>84.050</td>\n",
       "      <td>82.4700</td>\n",
       "      <td>84.000</td>\n",
       "      <td>62401194</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-29</th>\n",
       "      <td>82.870</td>\n",
       "      <td>84.550</td>\n",
       "      <td>82.5500</td>\n",
       "      <td>84.180</td>\n",
       "      <td>54995895</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-28</th>\n",
       "      <td>82.800</td>\n",
       "      <td>83.480</td>\n",
       "      <td>81.6900</td>\n",
       "      <td>81.820</td>\n",
       "      <td>58228575</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-27</th>\n",
       "      <td>84.970</td>\n",
       "      <td>85.350</td>\n",
       "      <td>83.0000</td>\n",
       "      <td>83.040</td>\n",
       "      <td>57284035</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-23</th>\n",
       "      <td>83.250</td>\n",
       "      <td>85.780</td>\n",
       "      <td>82.9344</td>\n",
       "      <td>85.250</td>\n",
       "      <td>57433655</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-07</th>\n",
       "      <td>163.839</td>\n",
       "      <td>165.243</td>\n",
       "      <td>162.0310</td>\n",
       "      <td>162.554</td>\n",
       "      <td>46605900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-06</th>\n",
       "      <td>163.450</td>\n",
       "      <td>164.800</td>\n",
       "      <td>161.9370</td>\n",
       "      <td>163.254</td>\n",
       "      <td>51957780</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-05</th>\n",
       "      <td>166.883</td>\n",
       "      <td>167.126</td>\n",
       "      <td>164.3570</td>\n",
       "      <td>164.357</td>\n",
       "      <td>64302720</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-04</th>\n",
       "      <td>170.438</td>\n",
       "      <td>171.400</td>\n",
       "      <td>166.3490</td>\n",
       "      <td>167.522</td>\n",
       "      <td>70725160</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-01-03</th>\n",
       "      <td>167.550</td>\n",
       "      <td>170.704</td>\n",
       "      <td>166.1600</td>\n",
       "      <td>170.404</td>\n",
       "      <td>63869140</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>251 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "               Open     High       Low    Close    Volume\n",
       "Date                                                     \n",
       "2022-12-30   83.120   84.050   82.4700   84.000  62401194\n",
       "2022-12-29   82.870   84.550   82.5500   84.180  54995895\n",
       "2022-12-28   82.800   83.480   81.6900   81.820  58228575\n",
       "2022-12-27   84.970   85.350   83.0000   83.040  57284035\n",
       "2022-12-23   83.250   85.780   82.9344   85.250  57433655\n",
       "...             ...      ...       ...      ...       ...\n",
       "2022-01-07  163.839  165.243  162.0310  162.554  46605900\n",
       "2022-01-06  163.450  164.800  161.9370  163.254  51957780\n",
       "2022-01-05  166.883  167.126  164.3570  164.357  64302720\n",
       "2022-01-04  170.438  171.400  166.3490  167.522  70725160\n",
       "2022-01-03  167.550  170.704  166.1600  170.404  63869140\n",
       "\n",
       "[251 rows x 5 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4 = pd.read_excel('data/2022年股票数据.xlsx', \n",
    "                    sheet_name='AMZN',  # 指定工作表\n",
    "                    index_col='Date') # 设置行索引\n",
    "df4"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e7334da-0e17-4f1f-a39c-b6464edc38f7",
   "metadata": {},
   "source": [
    "### 读取数据库"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "289667e0-5cc3-44f2-93f7-9cbfdeac7b92",
   "metadata": {},
   "source": [
    "## DataFrame的属性和方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "00b63b4f-9c7a-4dd7-87a0-16a1d1050911",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1001</th>\n",
       "      <td>62</td>\n",
       "      <td>95</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1002</th>\n",
       "      <td>72</td>\n",
       "      <td>65</td>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1003</th>\n",
       "      <td>93</td>\n",
       "      <td>86</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1004</th>\n",
       "      <td>88</td>\n",
       "      <td>66</td>\n",
       "      <td>69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1005</th>\n",
       "      <td>93</td>\n",
       "      <td>87</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      语文  数学  英语\n",
       "1001  62  95  66\n",
       "1002  72  65  75\n",
       "1003  93  86  82\n",
       "1004  88  66  69\n",
       "1005  93  87  82"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96533373-d3a7-422a-bcb2-1dc977f7d025",
   "metadata": {},
   "source": [
    "### 属性\n",
    "\n",
    "- at / iat\t通过标签获取DataFrame中的单个值。\n",
    "- columns\tDataFrame对象列的索引\n",
    "- dtypes\tDataFrame对象每一列的数据类型\n",
    "- empty\tDataFrame对象是否为空\n",
    "- loc / iloc\t通过标签获取DataFrame中的一组值。\n",
    "- ndim\tDataFrame对象的维度\n",
    "- shape\tDataFrame对象的形状（行数和列数）\n",
    "- size\tDataFrame对象中元素的个数\n",
    "- values\tDataFrame对象的数据对应的二维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "00f1e994-b766-40e8-9d67-22bca8d3ef26",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['语文', '数学', '英语'], dtype='object')"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列标签\n",
    "df2.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "3d0a7a49-1613-462a-ae31-2f915bed6015",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "语文    int64\n",
       "数学    int64\n",
       "英语    int64\n",
       "dtype: object"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据类型\n",
    "df2.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "1cb66604-7652-409d-af8e-495b703ba06f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 维度\n",
    "df2.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "0918f334-9371-406d-9e19-07c5cf2c0381",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[62, 95, 66],\n",
       "        [72, 65, 75],\n",
       "        [93, 86, 82],\n",
       "        [88, 66, 69],\n",
       "        [93, 87, 82]]),\n",
       " numpy.ndarray)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将数据转换为数组\n",
    "df2.values, type(df2.values)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63378f9d-7d5b-4c2b-8583-4e890fcf4322",
   "metadata": {},
   "source": [
    "### 方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "8f7c95d8-87ca-451b-bad9-274ebc084180",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Index: 5 entries, 1001 to 1005\n",
      "Data columns (total 3 columns):\n",
      " #   Column  Non-Null Count  Dtype\n",
      "---  ------  --------------  -----\n",
      " 0   语文      5 non-null      int64\n",
      " 1   数学      5 non-null      int64\n",
      " 2   英语      5 non-null      int64\n",
      "dtypes: int64(3)\n",
      "memory usage: 160.0 bytes\n"
     ]
    }
   ],
   "source": [
    "# 简要列出DataFrame的信息\n",
    "df2.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "75373b9c-6316-4cca-9346-90b775703357",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1001</th>\n",
       "      <td>62</td>\n",
       "      <td>95</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      语文  数学  英语\n",
       "1001  62  95  66"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看前几行\n",
    "df2.head(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "73efc401-4b9d-4b88-9245-6f610583f9b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1005</th>\n",
       "      <td>93</td>\n",
       "      <td>87</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      语文  数学  英语\n",
       "1005  93  87  82"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看后几行\n",
    "df2.tail(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d45cb16a-2eee-40f0-939e-6df9c170d71a",
   "metadata": {},
   "source": [
    "## 索引和切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "5ce5e65e-81e7-4afb-8177-29bbe025685f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1001    62\n",
       "1002    72\n",
       "1003    93\n",
       "1004    88\n",
       "1005    93\n",
       "Name: 语文, dtype: int64"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取列\n",
    "df2.语文 # 使用类似于JS对象获取属性的写法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "dce815ef-d6bf-41e5-9a64-ad6f1ea7c472",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1001    62\n",
       "1002    72\n",
       "1003    93\n",
       "1004    88\n",
       "1005    93\n",
       "Name: 语文, dtype: int64"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2['语文'] # 使用python字典写法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "5076c6aa-9b84-4275-9001-9de2d81825e0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "语文    72\n",
       "数学    65\n",
       "英语    75\n",
       "Name: 1002, dtype: int64"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取行\n",
    "df2.iloc[1] # 隐式索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "c2dcda29-373d-47cc-9f8a-3e7189c254bf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "语文    72\n",
       "数学    65\n",
       "英语    75\n",
       "Name: 1002, dtype: int64"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.loc[1002] # 显示索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "a47c098a-ff66-4aab-83c3-9dd11a0f0adb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1001</th>\n",
       "      <td>62</td>\n",
       "      <td>95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1002</th>\n",
       "      <td>72</td>\n",
       "      <td>65</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1003</th>\n",
       "      <td>93</td>\n",
       "      <td>86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1004</th>\n",
       "      <td>88</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1005</th>\n",
       "      <td>93</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      语文  数学\n",
       "1001  62  95\n",
       "1002  72  65\n",
       "1003  93  86\n",
       "1004  88  66\n",
       "1005  93  87"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取多列（双层方括号）\n",
    "df2[['语文', '数学']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "c354bfa7-a316-4ab4-9d4d-d147bc1b3194",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1003</th>\n",
       "      <td>93</td>\n",
       "      <td>86</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1004</th>\n",
       "      <td>88</td>\n",
       "      <td>66</td>\n",
       "      <td>69</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      语文  数学  英语\n",
       "1003  93  86  82\n",
       "1004  88  66  69"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取多行（双层方括号）\n",
    "df2.iloc[[2, 3]] # 隐式索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "8f9c341f-70fe-49c9-a202-c26048016823",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1001</th>\n",
       "      <td>62</td>\n",
       "      <td>95</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1005</th>\n",
       "      <td>93</td>\n",
       "      <td>87</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      语文  数学  英语\n",
       "1001  62  95  66\n",
       "1005  93  87  82"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.loc[[1001, 1005]] # 显示索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "7a2d7b05-5718-49e3-88cd-57e19323d0f2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(np.int64(93), np.int64(93))"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取指定单元格，行在前列在后\n",
    "df2.loc[1003, '语文'], df2.loc[1003]['语文']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "1c5a506c-4aa5-4828-beff-63d7c204af98",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1001</th>\n",
       "      <td>62</td>\n",
       "      <td>95</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1002</th>\n",
       "      <td>72</td>\n",
       "      <td>65</td>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1003</th>\n",
       "      <td>60</td>\n",
       "      <td>86</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1004</th>\n",
       "      <td>88</td>\n",
       "      <td>66</td>\n",
       "      <td>69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1005</th>\n",
       "      <td>93</td>\n",
       "      <td>87</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      语文  数学  英语\n",
       "1001  62  95  66\n",
       "1002  72  65  75\n",
       "1003  60  86  82\n",
       "1004  88  66  69\n",
       "1005  93  87  82"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 修改值\n",
    "df2.loc[1003, '语文'] = 60\n",
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "a8d39bee-4c71-46e4-bd8f-010f70bd2ef7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1002</th>\n",
       "      <td>72</td>\n",
       "      <td>65</td>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1003</th>\n",
       "      <td>60</td>\n",
       "      <td>86</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1004</th>\n",
       "      <td>88</td>\n",
       "      <td>66</td>\n",
       "      <td>69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1005</th>\n",
       "      <td>93</td>\n",
       "      <td>87</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      语文  数学  英语\n",
       "1002  72  65  75\n",
       "1003  60  86  82\n",
       "1004  88  66  69\n",
       "1005  93  87  82"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 切片\n",
    "df2.loc[1002:1005]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "4b4301de-6efb-4711-a79f-6ce3f5081245",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1005</th>\n",
       "      <td>93</td>\n",
       "      <td>87</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      语文  数学  英语\n",
       "1005  93  87  82"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 布尔索引筛选数据\n",
    "df2[df2.语文 > 90]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "a49e9546-8398-40fc-91a8-30e31cd113c4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1002</th>\n",
       "      <td>72</td>\n",
       "      <td>65</td>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1004</th>\n",
       "      <td>88</td>\n",
       "      <td>66</td>\n",
       "      <td>69</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      语文  数学  英语\n",
       "1002  72  65  75\n",
       "1004  88  66  69"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2[(df2.语文 < 90) & (df2.数学 < 80)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "fffb4486-1339-4aa8-8f02-1ec9f374bf8d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1003</th>\n",
       "      <td>60</td>\n",
       "      <td>86</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      语文  数学  英语\n",
       "1003  60  86  82"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2[df2.语文 == 60]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "366f08ae-23ab-46aa-93ab-4326170284dd",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
